package cn.iocoder.yudao.module.system.controller.admin.scorelineconfig;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.system.controller.admin.scorelineconfig.vo.ScoreLineConfigPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.scorelineconfig.vo.ScoreLineConfigRespVO;
import cn.iocoder.yudao.module.system.controller.admin.scorelineconfig.vo.ScoreLineConfigSaveReqVO;
import cn.iocoder.yudao.module.system.convert.scorelineconfig.ScoreLineConfigConvert;
import cn.iocoder.yudao.module.system.dal.dataobject.scorelineconfig.ScoreLineConfigDO;
import cn.iocoder.yudao.module.system.service.scorelineconfig.ScoreLineConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 分数线配置")
@RestController
@RequestMapping("/system/score-line-config")
@Validated
public class ScoreLineConfigController {

    @Resource
    private ScoreLineConfigService scoreLineConfigService;

    @PostMapping("/create")
    @Operation(summary = "创建分数线配置")
    @PreAuthorize("@ss.hasPermission('system:score-line-config:create')")
    public CommonResult<Long> createScoreLineConfig(@Valid @RequestBody ScoreLineConfigSaveReqVO createReqVO) {
        return success(scoreLineConfigService.createScoreLineConfig(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新分数线配置")
    @PreAuthorize("@ss.hasPermission('system:score-line-config:update')")
    public CommonResult<Boolean> updateScoreLineConfig(@Valid @RequestBody ScoreLineConfigSaveReqVO updateReqVO) {
        scoreLineConfigService.updateScoreLineConfig(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除分数线配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:score-line-config:delete')")
    public CommonResult<Boolean> deleteScoreLineConfig(@RequestParam("id") Long id) {
        scoreLineConfigService.deleteScoreLineConfig(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得分数线配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:score-line-config:query')")
    public CommonResult<ScoreLineConfigRespVO> getScoreLineConfig(@RequestParam("id") Long id) {
        ScoreLineConfigDO scoreLineConfig = scoreLineConfigService.getScoreLineConfig(id);
        return success(ScoreLineConfigConvert.INSTANCE.convert(scoreLineConfig));
    }

    @GetMapping("/page")
    @Operation(summary = "获得分数线配置分页")
    @PreAuthorize("@ss.hasPermission('system:score-line-config:query')")
    public CommonResult<PageResult<ScoreLineConfigRespVO>> getScoreLineConfigPage(@Valid ScoreLineConfigPageReqVO pageReqVO) {
        PageResult<ScoreLineConfigDO> pageResult = scoreLineConfigService.getScoreLineConfigPage(pageReqVO);
        return success(ScoreLineConfigConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出分数线配置 Excel")
    @PreAuthorize("@ss.hasPermission('system:score-line-config:export')")
    public void exportScoreLineConfigExcel(@Valid ScoreLineConfigPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ScoreLineConfigDO> list = scoreLineConfigService.getScoreLineConfigPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "分数线配置.xls", "数据", ScoreLineConfigRespVO.class,
                        ScoreLineConfigConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/list-by-subject-type")
    @Operation(summary = "根据科目类型获取分数线配置")
    @Parameter(name = "subjectType", description = "科目类型", required = true, example = "PHYSICS")
    @PreAuthorize("@ss.hasPermission('system:score-line-config:query')")
    public CommonResult<List<ScoreLineConfigRespVO>> getScoreLinesBySubjectType(@RequestParam("subjectType") String subjectType) {
        List<ScoreLineConfigDO> list = scoreLineConfigService.getScoreLinesBySubjectType(subjectType);
        return success(ScoreLineConfigConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/list-by-config-type")
    @Operation(summary = "根据配置类型获取分数线配置")
    @Parameter(name = "configType", description = "配置类型", required = true, example = "985")
    @PreAuthorize("@ss.hasPermission('system:score-line-config:query')")
    public CommonResult<List<ScoreLineConfigRespVO>> getScoreLinesByConfigType(@RequestParam("configType") String configType) {
        List<ScoreLineConfigDO> list = scoreLineConfigService.getScoreLinesByConfigType(configType);
        return success(ScoreLineConfigConvert.INSTANCE.convertList(list));
    }

    @PostMapping("/refresh-cache")
    @Operation(summary = "刷新分数线配置缓存")
    @PreAuthorize("@ss.hasPermission('system:score-line-config:update')")
    public CommonResult<Boolean> refreshScoreLineCache() {
        scoreLineConfigService.refreshScoreLineCache();
        return success(true);
    }

}